package com.ktjy.goods.util;

import lombok.Data;

/**
 * 控制器的通用返回类
 * @param <T>
 */
@Data
public class ResultUtil<T> {
    private String code;//看请求是否成功 -1代表失败  0代表成功
    private String msg;//返回给前端信息
    private T data;//返回集合、对象等等类型
    private long dq;//返回当前页
    private long pages;//返回共有多少页
    private long total;//返回共有多少条


    public ResultUtil() {
    }

    public ResultUtil(T data) {
        this.data = data;
    }

    //只返回消息不带数据
    public static ResultUtil success() {
        ResultUtil result = new ResultUtil<>();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }
    //返回数据不带分页
    public static <T> ResultUtil<T> success(T data) {
        ResultUtil<T> result = new ResultUtil<>(data);
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }
    //返回错误信息
    public static ResultUtil error(String msg) {
        ResultUtil result = new ResultUtil();
        result.setCode("-1");
        result.setMsg(msg);
        return result;
    }

    //返回带分页的数据
    public static <T> ResultUtil page(long dq, long pages, long total, T data){
        ResultUtil<T> result = new ResultUtil<>(data);
        result.setCode("0");
        result.setMsg("成功");
        result.setDq(dq);
        result.setPages(pages);
        result.setTotal(total);
        return result;
    }


}
